package com.feng.chat.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.feng.chat.common.Common;
import com.feng.chat.dto.KeepAlive;
import com.feng.chat.dto.RecordsDTO;
import com.feng.chat.entity.*;
import com.feng.chat.service.*;
import com.feng.chat.service.impl.*;
import com.feng.chat.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MessageUtil {

    private static final ITalkRecordsFileService talkRecordsFileService=new TalkRecordsFileServiceImpl();

    private static final ITalkRecordsForwardService talkRecordsForwardService=new TalkRecordsForwardServiceImpl();

    private static final ITalkRecordsCodeService talkRecordsCodeService=new TalkRecordsCodeServiceImpl();

    private static final ITalkRecordsVoteService talkRecordsVoteService=new TalkRecordsVoteServiceImpl();

    private static final IGroupNoticeService groupNoticeService=new GroupNoticeServiceImpl();

    private static final ITalkRecordsLoginService talkRecordsLoginService=new TalkRecordsLoginServiceImpl();

    private static final ITalkRecordsInviteService talkRecordsInviteService=new TalkRecordsInviteServiceImpl();

    private static final ITalkRecordsLocationService talkRecordsLocationService=new TalkRecordsLocationServiceImpl();


    private static final ITalkRecordsService talkRecordsService=new TalkRecordsServiceImpl();

    private static final IUsersService usersService=new UsersServiceImpl();

    /**
     * 判断消息类型
     * 消息类型[1:文本消息;2:文件消息;3:会话消息;4:代码消息;5:投票消息;6:群公告;7:好友申请;8:登录通知;9:入群消息/退群消息;]
     *
     * @param msgType
     * @return
     */
    public static String getMessageType(Integer msgType, String content) {
        String result = "";
        switch (msgType) {
            case 0:
            case 1:
                result = content;
                break;
            case 2:
                result = "[文件消息]";
                break;
            case 3:
                result = "[会话消息]";
                break;
            case 4:
                result = "[代码消息]";
                break;
            case 5:
                result = "[投票消息]";
                break;
            case 6:
                result = "[群公告]";
                break;
            case 7:
                result = "[好友申请]";
                break;
            case 8:
                result = "[登录通知]";
                break;
            case 9:
                result = "[入群消息/退群消息]";
                break;
        }
        return result;
    }

    /**
     * 通过records_id获取特殊消息内容
     *
     * @param talkRecords
     * @return
     */
    public static Object getRecordsType(TalkRecords talkRecords) {
        switch (talkRecords.getMsgType()) {
            case 1: //文本消息
                return talkRecords;
            case 2: //文件消息
                QueryWrapper<TalkRecordsFile> wrapper = new QueryWrapper<>();
                wrapper.eq("record_id", talkRecords.getId());
                TalkRecordsFile recordsFile = talkRecordsFileService.getOne(wrapper);
                return recordsFile;
            case 3: //会话消息
                QueryWrapper<TalkRecordsForward> wrapperForward = new QueryWrapper<>();
                wrapperForward.eq("record_id", talkRecords.getId());
                TalkRecordsForward recordsForward = talkRecordsForwardService.getOne(wrapperForward);
                return recordsForward;
            case 4: //代码消息
                QueryWrapper<TalkRecordsCode> wrapperCode = new QueryWrapper<>();
                wrapperCode.eq("record_id", talkRecords.getId());
                TalkRecordsCode recordsCode = talkRecordsCodeService.getOne(wrapperCode);
                return recordsCode;
            case 5: //投票消息
                QueryWrapper<TalkRecordsVote> wrapperVote = new QueryWrapper<>();
                wrapperVote.eq("record_id", talkRecords.getId());
                TalkRecordsVote recordsVote = talkRecordsVoteService.getOne(wrapperVote);
                return recordsVote;
            case 6: //群公告消息
                QueryWrapper<GroupNotice> wrapperGroupNotice = new QueryWrapper<>();
                wrapperGroupNotice.eq("record_id", talkRecords.getId());
                GroupNotice groupNotice = groupNoticeService.getOne(wrapperGroupNotice);
                return groupNotice;
            case 8: //登录通知消息
                QueryWrapper<TalkRecordsLogin> wrapperLogin = new QueryWrapper<>();
                wrapperLogin.eq("record_id", talkRecords.getId());
                TalkRecordsLogin recordsLogin = talkRecordsLoginService.getOne(wrapperLogin);
                return recordsLogin;
            case 9: //入群/退群消息
                QueryWrapper<TalkRecordsInvite> wrapperInvite = new QueryWrapper<>();
                wrapperInvite.eq("record_id", talkRecords.getId());
                TalkRecordsInvite recordsInvite = talkRecordsInviteService.getOne(wrapperInvite);
                return recordsInvite;
            case 10: //位置消息
                QueryWrapper<TalkRecordsLocation> wrapperLocation = new QueryWrapper<>();
                wrapperLocation.eq("record_id", talkRecords.getId());
                TalkRecordsLocation recordsLocation = talkRecordsLocationService.getOne(wrapperLocation);
                return recordsLocation;
        }
        return null;
    }

}
